package com.huli.controller;

import cn.hutool.core.lang.Assert;
import com.huli.dto.MailDto;
import com.huli.dto.CheckMailDto;
import com.huli.service.IOwnerService;
import com.huli.service.IUserService;
import com.huli.service.MailSenderService;
import com.huli.domain.Owner;
import com.huli.domain.User;
import com.huli.utils.JwtUtils;
import com.huli.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.Date;

@RestController
@Api("邮箱接口")
public class MailController {

    @Autowired
    MailSenderService mailSenderService;

    @Autowired
    IUserService userService;

    @Autowired
    IOwnerService ownerService;

    /**
     * 发送验证码
     *
     * @param to 需要发送地址
     * @return
     * @throws MessagingException
     */
    @PostMapping("sendCheckMail")
    @ApiOperation("发送验证码邮件")
    public Result sendCheckMail(@RequestParam("to") String to) throws MessagingException {
        try {
            mailSenderService.sendCheckMail(to);
        } catch (MessagingException e) {
            throw new MessagingException(e.getMessage());
        }

        return Result.successResult(null);
    }

    /**
     * 发送自定义邮件
     *
     * @param mail
     * @return
     */
    @PostMapping("sendSimpleMail")
    @ApiOperation("发送自定义邮件")
    public Result sendSimpleMail(MailDto mail) {
        mailSenderService.sendSimpleMail(mail);

        return Result.successResult(null);
    }

    @PostMapping("sendCheckUserMail")
    @ApiOperation("发送验证邮箱链接邮件")
    public Result sendCheckUserMail(@RequestBody CheckMailDto checkMailDto) {

        try {
            mailSenderService.sendCheckUserMail(checkMailDto);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return Result.successResult(null);
    }

    @GetMapping("checkMail")
    public Result checkMail(@RequestParam("token") String token, @RequestParam("type") Integer type) throws IllegalArgumentException {
        JwtUtils jwtUtils = new JwtUtils();
        boolean isExpired = jwtUtils.isTokenExpired(new Date());
        Assert.isTrue(isExpired, "JwtToken过期!");

        Claims claim = jwtUtils.getClaimByToken(token);
        switch (type) {
            case 1: {
                User user = new User();
                user.setUserId(Integer.valueOf(claim.getSubject()));
                user.setUserCheck(3);
                boolean b = userService.updateById(user);
                Assert.isTrue(b, "审核邮箱失败");
            }
            break;
            case 2: {
                Owner owner = new Owner();
                owner.setOwnerId(Integer.valueOf(claim.getSubject()));
                owner.setOwnerCheck(3);
                boolean b = ownerService.updateById(owner);
                Assert.isTrue(b, "审核邮箱失败");
            }
            break;
        }


        return Result.successResult(null);
    }

}
